package test.aspect;

import java.util.Arrays;

import javax.annotation.PostConstruct;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.Assert;

@Aspect
public class ImprovedLoggingAspect {
	private String beforMessage;
	private String afterMessage;

	@Around("execution(* test.aspect.TestBean.*(..))")
	public Object log(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println(String.format(this.beforMessage, 
				pjp.getSignature().getName(),Arrays.toString(pjp.getArgs())));
		
		System.out.println("this.beforMessage = "+ this.beforMessage);
		System.out.println("pjp.getSignature().getName() = "+ pjp.getSignature().getName());
		System.out.println("pjp.getArgs() = "+ Arrays.toString(pjp.getArgs()));
		
		Object ret = pjp.proceed();

		System.out.println(String.format(this.afterMessage, pjp.getSignature()
				.getName(),Arrays.toString(pjp.getArgs())));

		return ret;
	}
	
	/**
	@PostConstruct
	public void initialize(){
		Assert.notNull(this.beforMessage,
				"The [beforMessage] property of [" +
		getClass().getName()+ "]must be set.");
		
		Assert.notNull(this.afterMessage,
				"The [afterMessage] property of [" +
		getClass().getName()+ "]must be set.");
		
	}*/

	public void setBeforMessage(String beforMessage) {
		this.beforMessage = beforMessage;
	}

	public void setAfterMessage(String afterMessage) {
		this.afterMessage = afterMessage;
	}
	
	
}
